<?php

/*
 *
 *
 * This software has been made FREE by author for the interest of other developers. 
 *      I hope this will help you in coding faster and robust php code
 *            You can modify and redistribute it as you like.
 *              Your comments, suggestions and corrections 
 *                          will be highly 
 *                           APPRECIATED. 
 *                                      Author
 * @author :   Roshan Budhathoki (developer.roshan@gmail.com);
 * @URL    :   <http://www.roshanbudhathoki.com>
 *
 */
if (! Params::getParam ( 'action' )) {
	Params::setParam ( 'action', 'index' );
}

switch (Params::getParam ( 'action' )) {
	case 'index' :
		include cp_template_path () . 'login.php';
		break;
	case 'login' :
		if (! isset ( $_POST ['username'] ) || $_POST ['username'] == '') {
			redirect ( cp_url () . '?page=login' );
		}
		if (! isset ( $_POST ['password'] ) || $_POST ['password'] == '') {
			redirect ( cp_url () . '?page=login' );
		}
		
		$username = trim ( $_POST ['username'] );
		$password = $_POST ['password'];
		$user = Model_Mapper_Admin::newInstance ()->getByUserName ( $username );
		
		if ($user) {
			if (strcmp ( sha1 ( $password ), $user->getPassword () ) === 0) {
				Session::newInstance ()->_set ( 'admin_id', $user->getAdmin_id () );
				Session::newInstance ()->_set ( 'admin', $user );
			
			}
			
			redirect ( cp_url () );
		} else {
			
			redirect ( cp_url () );
		}
		
		break;
	case 'logout' :
		unset ( $_SESSION ['admin_id'] );
		redirect(cp_url());
		break;
	default :
		echo 'UNRECOGNIZED ACTION';
		break;
}
